#!/usr/bin/perl -w	# useful for syntax highlighting
package clreqd_modules;

use cldb;
use pidlock;
use C_client2;
use Sys::Hostname;
new clmasterdefaults;

#########################################################################
# This utility is used to KILL all daemons on the current node.		#
# Mainly used when things really get nasty and the software needs to	#
# be terminated.							#
# in the form: [lha_]daemon[d],...					#
#									#
# Output is:								#
# OK N   [N is number of processes found/killed]			#
#									#
#########################################################################

sub kill_all_daemons {
my %ARGS=@_;
my @D=qw(hbd lockd netd lockd syncd statd mond clusterd cldbd clstatdbd);

	local $SIG{CHLD}='DEFAULT';
	# We look for processes with the name $ENV{TRUECL_CODE/bin}/lha_...
	my $p="$::ENV{TRUECL_CODE}/bin/lha_";
	my $ps=`ps -ef`;
	my $killed=0;
	my $killed2=0;
	# we ABORT the cldb and clstatdb ports via the defaults or hard coded
	# port values and attempt to kill them off (if PING OK...)

	{
		my $defaults=new clmasterdefaults("$::ENV{TRUECL_CFG}/master-defaults.xml");
		if(!defined($defaults)) {
			error(clmasterdefaults::error(),1);
		}
		my $port;
		my $xx=$defaults->get_default_value("configdb_port",1);
		if(defined($xx)) {
			$port=$xx;
		} else {
			::warning("Port not found for configuration database in defaults.");
			::warning("Defaulting to 11883.");
			$port=11883;
		}
		my $client=new C_client2(SERVER_NAME => hostname,SERVER_INET => "127.0.01",
				SERVER_PORT=>$port, PKI=>$::PKI);
		if(!defined($client)) {
			::warning("Unable to gain connection to configuration database service.");
		} else {
			$client->set_params(TIMEOUT => 20);

			my ($resp,$err)=$client->send_request("ABORT");
			if(defined($resp) && $resp =~ /^OK/) {
				$killed2++;
			}
		}
		$client=undef;

		$xx=$defaults->get_default_value("statdb_port",1);
		if(defined($xx)) {
			$port=$xx;
		} else {
			::warning("Port not found for status database in defaults.");
			::warning("Defaulting to 11884.");
			$port=11884;
		}
		$client=new C_client2(SERVER_NAME => hostname,SERVER_INET => "127.0.01",
				SERVER_PORT=>$port, PKI=>$::PKI);
		if(!defined($client)) {
			::warning("Unable to gain connection to status database service.");
		} else {
			$client->set_params(TIMEOUT => 20);

			my ($resp,$err)=$client->send_request("ABORT");
			if(defined($resp) && $resp =~ /^OK/) {
				$killed2++;
			}
		}
	}
	my @PI=();
	for (split(/\n/,$ps)) {
		next if !defined($_);
		next if ! length($_);
		my @a=split;
		my $pid=$a[1];
		my $proc;
P: {
		for $proc (@D) {
			for (7 .. 12) {
				last if (!exists($a[$_]) || !defined($a[$_]));
				if($a[$_] eq "$p$proc") {
					kill 1,$pid;
					push @PI,$pid;
					$killed++;
					last P;
				}
			}
		}
}

	}
	for my $cpid (@PI) {
		if(kill 0,$cpid) {
			kill 15,$cpid;
		}
	}
	return "OK $killed2" if !$killed;
	$killed+=$killed2;
	# Repeat the process after .5 seconds, and kill off any remaining...
	select(undef,undef,undef,0.5);
	for (split(/\n/,$ps)) {
		next if !defined($_);
		next if ! length($_);
		my @a=split;
		my $pid=$a[1];
		my $proc;
P2: {
		for $proc (@D) {
			for (7 .. 12) {
				last if (!exists($a[$_]) || !defined($a[$_]));
				if($a[$_] eq "$p$proc") {
					kill 9,$pid;
					last P2;
				}
			}
		}
}
	}

	return "OK $killed";
}

1;
